package com.example.dangerouscargodriver.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.bumptech.glide.Glide;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class EvaluateOrderActivity extends HttpRequestActivity implements View.OnClickListener {

    private static final String TAG = "EvaluateOrderActivity";
    private static final String ADVERTISE = "https://cdn.vsiji.cn/image/Public/Images/upload/2339/2105/df442dd50a7dd2843f2fad0c749e08ea.png";
    private Button mEvaluate = null;
    private ActionBar mActionBar = null;

    private ImageView mStartOff1 = null;
    private ImageView mStartOff2 = null;
    private ImageView mStartOff3 = null;
    private ImageView mStartOff4 = null;
    private ImageView mStartOff5 = null;
    private ImageView mStartOn1 = null;
    private ImageView mStartOn2 = null;
    private ImageView mStartOn3 = null;
    private ImageView mStartOn4 = null;
    private ImageView mStartOn5 = null;

    private TextView mTipA = null;
    private TextView mTipB = null;
    private TextView mTipC = null;
    private TextView mTipD = null;
    private TextView mTipE = null;

    private TextView mTip1 = null;
    private TextView mTip2 = null;
    private TextView mTip3 = null;
    private TextView mTip4 = null;
    private TextView mTip5 = null;
    private TextView mTip6 = null;
    private TextView mTip7 = null;
    private TextView mTip8 = null;
    private TextView mTip9 = null;

    private boolean mTipFlag_1 = false;
    private boolean mTipFlag_2 = false;
    private boolean mTipFlag_3 = false;
    private boolean mTipFlag_4 = false;
    private boolean mTipFlag_5 = false;
    private boolean mTipFlag_6 = false;
    private boolean mTipFlag_7 = false;
    private boolean mTipFlag_8 = false;
    private boolean mTipFlag_9 = false;

    private LinearLayout mLevelA = null;
    private LinearLayout mLevelB = null;
    private LinearLayout mCommentTop = null;
    private LinearLayout mCommentBottom = null;
    private ImageView mAdvertiseUrl = null;

    private String mId = null;
    private String mContent = null;
    private int mScore = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(EvaluateOrderActivity.class);
        setContentView(R.layout.activity_evaluate_order);
        mId = getIntent().getStringExtra("id");

        mStartOff1 = findViewById(R.id.comment_star_off_1);
        mStartOff2 = findViewById(R.id.comment_star_off_2);
        mStartOff3 = findViewById(R.id.comment_star_off_3);
        mStartOff4 = findViewById(R.id.comment_star_off_4);
        mStartOff5 = findViewById(R.id.comment_star_off_5);
        mStartOn1 = findViewById(R.id.comment_star_on_1);
        mStartOn2 = findViewById(R.id.comment_star_on_2);
        mStartOn3 = findViewById(R.id.comment_star_on_3);
        mStartOn4 = findViewById(R.id.comment_star_on_4);
        mStartOn5 = findViewById(R.id.comment_star_on_5);
        mStartOff1.setOnClickListener(this);
        mStartOff2.setOnClickListener(this);
        mStartOff3.setOnClickListener(this);
        mStartOff4.setOnClickListener(this);
        mStartOff5.setOnClickListener(this);
        mStartOn1.setOnClickListener(this);
        mStartOn2.setOnClickListener(this);
        mStartOn3.setOnClickListener(this);
        mStartOn4.setOnClickListener(this);
        mStartOn5.setOnClickListener(this);
        mTipA = findViewById(R.id.text_comment_tipA);
        mTipB = findViewById(R.id.text_comment_tipB);
        mTipC = findViewById(R.id.text_comment_tipC);
        mTipD = findViewById(R.id.text_comment_tipD);
        mTipE = findViewById(R.id.text_comment_tipE);
        mLevelA = findViewById(R.id.layout_comment_level_A);
        mLevelB = findViewById(R.id.layout_comment_level_B);
        mTip1 = findViewById(R.id.comment_tip_1);
        mTip2 = findViewById(R.id.comment_tip_2);
        mTip3 = findViewById(R.id.comment_tip_3);
        mTip4 = findViewById(R.id.comment_tip_4);
        mTip5 = findViewById(R.id.comment_tip_5);
        mTip6 = findViewById(R.id.comment_tip_6);
        mTip7 = findViewById(R.id.comment_tip_7);
        mTip8 = findViewById(R.id.comment_tip_8);
        mTip9 = findViewById(R.id.comment_tip_9);

        mCommentTop = findViewById(R.id.layout_complete_comment_top);
        mCommentBottom = findViewById(R.id.layout_complete_comment_bottom);

        int commentType = getIntent().getIntExtra("type",1);
        if(commentType == 1)
            mCommentTop.setVisibility(View.VISIBLE);
        else
            mCommentBottom.setVisibility(View.VISIBLE);

        View.OnClickListener tipClick = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.comment_tip_1:
                        if(!mTipFlag_1) {
                            mTip1.setTextColor(getResources().getColor(R.color.black));
                            mTip1.setBackground(getResources().getDrawable(R.color.orange));
                            mTipFlag_1 = true;
                        }else{
                            mTip1.setTextColor(getResources().getColor(R.color.cp_color_gray));
                            mTip1.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
                            mTipFlag_1 = false;
                        }
                        break;
                    case R.id.comment_tip_2:
                        if(!mTipFlag_2) {
                            mTip2.setTextColor(getResources().getColor(R.color.black));
                            mTip2.setBackground(getResources().getDrawable(R.color.orange));
                            mTipFlag_2 = true;
                        }else{
                            mTip2.setTextColor(getResources().getColor(R.color.cp_color_gray));
                            mTip2.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
                            mTipFlag_2 = false;
                        }
                        break;
                    case R.id.comment_tip_3:
                        if(!mTipFlag_3) {
                            mTip3.setTextColor(getResources().getColor(R.color.black));
                            mTip3.setBackground(getResources().getDrawable(R.color.orange));
                            mTipFlag_3 = true;
                        }else{
                            mTip3.setTextColor(getResources().getColor(R.color.cp_color_gray));
                            mTip3.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
                            mTipFlag_3 = false;
                        }
                        break;
                    case R.id.comment_tip_4:
                        if(!mTipFlag_4) {
                            mTip4.setTextColor(getResources().getColor(R.color.black));
                            mTip4.setBackground(getResources().getDrawable(R.color.orange));
                            mTipFlag_4 = true;
                        }else{
                            mTip4.setTextColor(getResources().getColor(R.color.cp_color_gray));
                            mTip4.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
                            mTipFlag_4 = false;
                        }
                        break;
                    case R.id.comment_tip_5:
                        if(!mTipFlag_5) {
                            mTip5.setTextColor(getResources().getColor(R.color.black));
                            mTip5.setBackground(getResources().getDrawable(R.color.orange));
                            mTipFlag_5 = true;
                        }else{
                            mTip5.setTextColor(getResources().getColor(R.color.cp_color_gray));
                            mTip5.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
                            mTipFlag_5 = false;
                        }
                        break;
                    case R.id.comment_tip_6:
                        if(!mTipFlag_6) {
                            mTip6.setTextColor(getResources().getColor(R.color.black));
                            mTip6.setBackground(getResources().getDrawable(R.color.orange));
                            mTipFlag_6 = true;
                        }else{
                            mTip6.setTextColor(getResources().getColor(R.color.cp_color_gray));
                            mTip6.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
                            mTipFlag_6 = false;
                        }
                        break;
                    case R.id.comment_tip_7:
                        if(!mTipFlag_7) {
                            mTip7.setTextColor(getResources().getColor(R.color.black));
                            mTip7.setBackground(getResources().getDrawable(R.color.orange));
                            mTipFlag_7 = true;
                        }else{
                            mTip7.setTextColor(getResources().getColor(R.color.cp_color_gray));
                            mTip7.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
                            mTipFlag_7 = false;
                        }
                        break;
                    case R.id.comment_tip_8:
                        if(!mTipFlag_8) {
                            mTip8.setTextColor(getResources().getColor(R.color.black));
                            mTip8.setBackground(getResources().getDrawable(R.color.orange));
                            mTipFlag_8 = true;
                        }else{
                            mTip8.setTextColor(getResources().getColor(R.color.cp_color_gray));
                            mTip8.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
                            mTipFlag_8 = false;
                        }
                        break;
                    case R.id.comment_tip_9:
                        if(!mTipFlag_9) {
                            mTip9.setTextColor(getResources().getColor(R.color.black));
                            mTip9.setBackground(getResources().getDrawable(R.color.orange));
                            mTipFlag_9 = true;
                        }else{
                            mTip9.setTextColor(getResources().getColor(R.color.cp_color_gray));
                            mTip9.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
                            mTipFlag_9 = false;
                        }
                        break;
                }
            }
        };

        mTip1.setOnClickListener(tipClick);
        mTip2.setOnClickListener(tipClick);
        mTip3.setOnClickListener(tipClick);
        mTip4.setOnClickListener(tipClick);
        mTip5.setOnClickListener(tipClick);
        mTip6.setOnClickListener(tipClick);
        mTip7.setOnClickListener(tipClick);
        mTip8.setOnClickListener(tipClick);
        mTip9.setOnClickListener(tipClick);

        mAdvertiseUrl = findViewById(R.id.iv_advertise_url);
        Glide.with(this).load(ADVERTISE).into(mAdvertiseUrl);

        mEvaluate = findViewById(R.id.btn_evaluate_nameless);
        mEvaluate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkUserInput()) {
                    executeEvaluateOrderAction();
                }
            }
        });

        mActionBar = findViewById(R.id.actionBarEvaluateOrder);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void executeEvaluateOrderAction(){
        mContent = "";
        if(mTipFlag_1)
            mContent += "," + getResources().getString(R.string.tv_order_star_comment_tip_1);
        if(mTipFlag_2)
            mContent += "," + getResources().getString(R.string.tv_order_star_comment_tip_2);
        if(mTipFlag_3)
            mContent += "," + getResources().getString(R.string.tv_order_star_comment_tip_3);
        if(mTipFlag_4)
            mContent += "," + getResources().getString(R.string.tv_order_star_comment_tip_4);
        if(mTipFlag_5)
            mContent += "," + getResources().getString(R.string.tv_order_star_comment_tip_5);
        if(mTipFlag_6)
            mContent += "," + getResources().getString(R.string.tv_order_star_comment_tip_6);
        if(mTipFlag_7)
            mContent += "," + getResources().getString(R.string.tv_order_star_comment_tip_7);
        if(mTipFlag_8)
            mContent += "," + getResources().getString(R.string.tv_order_star_comment_tip_8);
        if(mTipFlag_9)
            mContent += "," + getResources().getString(R.string.tv_order_star_comment_tip_9);

        mContent = mContent.substring(1);
        HashMap<String, String> header = new HashMap<>();
        header.put("token", AuthController.Instance().getToken());

        JSONObject param = new JSONObject();
        try {
            param.put("id", mId);
            param.put("score", mScore);
            param.put("content", mContent);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Log.d(TAG, "param: " + param.toString());

        sendHttpJsonRequest(IHttpRequest.RequestMethod.PUT_METHOD,
                RemoteAPI.REQUEST_V1_USER_SG_MARK,
                header,
                param,
                RemoteAPICmd.REQUEST_V1_USER_SG_MARK);
    }

    @Override
    public void onClick(View v) {
        setInvisible();
        boolean isShowTip = false;
        boolean isShowLevel = false;
        switch (v.getId()) {
            case R.id.comment_star_off_5:
                mScore = 5;
                mStartOff5.setVisibility(View.INVISIBLE);
                mStartOn5.setVisibility(View.VISIBLE);
                if(!isShowTip){
                    isShowTip = true;
                    mTipE.setVisibility(View.VISIBLE);
                }
            case R.id.comment_star_off_4:
                mScore = mScore>4 ? mScore : 4;
                mStartOff4.setVisibility(View.INVISIBLE);
                mStartOn4.setVisibility(View.VISIBLE);
                if(!isShowTip){
                    isShowTip = true;
                    mTipD.setVisibility(View.VISIBLE);
                }
                if(!isShowLevel){
                    isShowLevel = true;
                    mLevelB.setVisibility(View.VISIBLE);
                    if(mTipFlag_1 || mTipFlag_2 || mTipFlag_3 || mTipFlag_4 || mTipFlag_5)
                        setTipUncheckA();
                }
            case R.id.comment_star_off_3:
                mScore = mScore>3 ? mScore : 3;
                mStartOff3.setVisibility(View.INVISIBLE);
                mStartOn3.setVisibility(View.VISIBLE);
                if(!isShowTip){
                    isShowTip = true;
                    mTipC.setVisibility(View.VISIBLE);
                }
                if(!isShowLevel){
                    isShowLevel = true;
                    mLevelA.setVisibility(View.VISIBLE);
                    if(mTipFlag_6 || mTipFlag_7 || mTipFlag_8 || mTipFlag_9)
                        setTipUncheckB();
                }
            case R.id.comment_star_off_2:
                mScore = mScore>2 ? mScore : 2;
                mStartOff2.setVisibility(View.INVISIBLE);
                mStartOn2.setVisibility(View.VISIBLE);
                if(!isShowTip){
                    isShowTip = true;
                    mTipB.setVisibility(View.VISIBLE);
                }
                if(!isShowLevel){
                    isShowLevel = true;
                    mLevelA.setVisibility(View.VISIBLE);
                    if(mTipFlag_6 || mTipFlag_7 || mTipFlag_8 || mTipFlag_9)
                        setTipUncheckB();
                }
            case R.id.comment_star_off_1:
                mScore = mScore>1 ? mScore : 1;;
                mStartOff1.setVisibility(View.INVISIBLE);
                mStartOn1.setVisibility(View.VISIBLE);
                if(!isShowTip){
                    mTipA.setVisibility(View.VISIBLE);
                }
                if(!isShowLevel){
                    mLevelA.setVisibility(View.VISIBLE);
                    if(mTipFlag_6 || mTipFlag_7 || mTipFlag_8 || mTipFlag_9)
                        setTipUncheckB();
                }
                break;
            case R.id.comment_star_on_1:
                mScore = 1;
                mStartOn2.setVisibility(View.INVISIBLE);
                mStartOff2.setVisibility(View.VISIBLE);
                if(!isShowTip) {
                    isShowTip = true;
                    mTipA.setVisibility(View.VISIBLE);
                }
                if(!isShowLevel){
                    isShowLevel = true;
                    mLevelA.setVisibility(View.VISIBLE);
                    if(mTipFlag_6 || mTipFlag_7 || mTipFlag_8 || mTipFlag_9)
                        setTipUncheckB();
                }
            case R.id.comment_star_on_2:
                mScore = mScore<2 ? mScore : 2;
                mStartOn3.setVisibility(View.INVISIBLE);
                mStartOff3.setVisibility(View.VISIBLE);
                if(!isShowTip) {
                    isShowTip = true;
                    mTipB.setVisibility(View.VISIBLE);
                }
                if(!isShowLevel){
                    isShowLevel = true;
                    mLevelA.setVisibility(View.VISIBLE);
                    if(mTipFlag_6 || mTipFlag_7 || mTipFlag_8 || mTipFlag_9)
                        setTipUncheckB();
                }
            case R.id.comment_star_on_3:
                mScore = mScore<3 ? mScore : 3;
                mStartOn4.setVisibility(View.INVISIBLE);
                mStartOff4.setVisibility(View.VISIBLE);
                if(!isShowTip) {
                    isShowTip = true;
                    mTipC.setVisibility(View.VISIBLE);
                }
                if(!isShowLevel){
                    isShowLevel = true;
                    mLevelA.setVisibility(View.VISIBLE);
                    if(mTipFlag_6 || mTipFlag_7 || mTipFlag_8 || mTipFlag_9)
                        setTipUncheckB();
                }
            case R.id.comment_star_on_4:
                mScore = mScore<4 ? mScore : 4;
                mStartOn5.setVisibility(View.INVISIBLE);
                mStartOff5.setVisibility(View.VISIBLE);
                if(!isShowTip) {
                    mTipD.setVisibility(View.VISIBLE);
                }
                if(!isShowLevel){
                    mLevelB.setVisibility(View.VISIBLE);
                    if(mTipFlag_1 || mTipFlag_2 || mTipFlag_3 || mTipFlag_4 || mTipFlag_5)
                        setTipUncheckA();
                }
                break;
            case R.id.comment_star_on_5:
                mScore = mScore<5 ? mScore : 5;
                mTipE.setVisibility(View.VISIBLE);
                mLevelB.setVisibility(View.VISIBLE);
                if(mTipFlag_1 || mTipFlag_2 || mTipFlag_3 || mTipFlag_4 || mTipFlag_5)
                    setTipUncheckA();
                break;
        }

    }

    private void setInvisible(){
        mLevelA.setVisibility(View.GONE);
        mLevelB.setVisibility(View.GONE);
        mTipA.setVisibility(View.GONE);
        mTipB.setVisibility(View.GONE);
        mTipC.setVisibility(View.GONE);
        mTipD.setVisibility(View.GONE);
        mTipE.setVisibility(View.GONE);
    }

    private void setTipUncheckA(){
        mTip1.setTextColor(getResources().getColor(R.color.cp_color_gray));
        mTip2.setTextColor(getResources().getColor(R.color.cp_color_gray));
        mTip3.setTextColor(getResources().getColor(R.color.cp_color_gray));
        mTip4.setTextColor(getResources().getColor(R.color.cp_color_gray));
        mTip5.setTextColor(getResources().getColor(R.color.cp_color_gray));
        mTip1.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
        mTip2.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
        mTip3.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
        mTip4.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
        mTip5.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
        mTipFlag_1 = false;
        mTipFlag_2 = false;
        mTipFlag_3 = false;
        mTipFlag_4 = false;
        mTipFlag_5 = false;

    }
    private void setTipUncheckB(){
        mTip6.setTextColor(getResources().getColor(R.color.cp_color_gray));
        mTip7.setTextColor(getResources().getColor(R.color.cp_color_gray));
        mTip8.setTextColor(getResources().getColor(R.color.cp_color_gray));
        mTip9.setTextColor(getResources().getColor(R.color.cp_color_gray));
        mTip6.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
        mTip7.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
        mTip8.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
        mTip9.setBackground(getResources().getDrawable(R.drawable.rectangle_edittext_home_bg));
        mTipFlag_6 = false;
        mTipFlag_7 = false;
        mTipFlag_8 = false;
        mTipFlag_9 = false;
    }

    private boolean checkUserInput(){
        if(mScore == 0){
            ToastUtils.showLongToast(this, "请选择评价星数等级");
            return false;
        }
        if(!mTipFlag_1 && !mTipFlag_2 && !mTipFlag_3 && !mTipFlag_4 && !mTipFlag_5 && !mTipFlag_5 &&
                !mTipFlag_6 && !mTipFlag_7 && !mTipFlag_8 && !mTipFlag_9){
            ToastUtils.showLongToast(this, "请至少选择一项描述信息");
            return false;
        }


        return true;
    }

    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            Log.d(TAG, "onResponse: " + response);
            if(actionId == RemoteAPICmd.REQUEST_V1_USER_SG_MARK){
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                ToastUtils.showLongToast(this, "评价完成");
                if(responseInfo != null && responseInfo.getStatus() == 1) {
                    //跳转到评价详情页面
                    finish();
                }else{
                    ToastUtils.showLongToast(this, "评价失败");
                }
            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {
        if(actionId == RemoteAPICmd.REQUEST_V1_USER_SG_MARK){
            ToastUtils.showLongToast(this, "评价失败");
        }
    }
}